import { GlobalEnum } from "../Config/GlobalEnum";
import EventManager from "../Managers/EventManager";
import { EventTypes } from "../Managers/EventTypes";
import { StorageSystem } from "../SystemStorage/StorageSystem";
import { SDKSystem } from "./SDKSystem";
import { WXGameService } from "./cxsdk/WXGameService";



/**
 * WX排行榜管理器
 */
export class RankMgr {
    private static instance: RankMgr;
    private constructor() { }
    public static get inst() {
        if (this.instance == null) {
            this.instance = new RankMgr();
        }
        return this.instance;
    }

    // appid = 'wx3a0a93c2acb0c513'; // （智力挑战场）

    get appid() {
        return SDKSystem.getCurAppid();
    }
    // 自身uid
    get rankUid() {
        return SDKSystem.getUserRankId();
    }

    get rankKey() {
        return 'shudu_rank_' + this.appid;
    }

    //#region ---世界排行数据
    //提交个人世界排行数据
    submitWorldScore(info: GlobalEnum.RankInfo, score, callback?: Function) {

        let userInfo = JSON.stringify(info);
        let promise: any = WXGameService.submitWorldScore(this.appid, this.rankUid, this.rankKey, score, userInfo);
        promise && promise.then(
            (res) => {
                if (callback) {
                    callback(res);
                }
            }
        ).catch(
            (err) => {
                console.log("上传世界排行数据执行失败：", err);
                let promise1: any = WXGameService.submitWorldScore(this.appid, this.rankUid, this.rankKey, score, userInfo);
                promise1.then(
                    (res) => {
                        if (callback) {
                            callback(res);
                        }
                    }
                ).catch(
                    (err) => {
                        console.log("上传世界排行数据再次执行失败：", err);
                    }
                );
            }
        );
    }

    //请求世界排行数据
    getWorldRankData(group, count, callback?: Function) {

        let promise = WXGameService.getWorldRankData(this.appid, this.rankUid, this.rankKey, group, count);
        promise && promise.then(
            (res) => {
                if (callback) {
                    callback(res);
                }
            }
        ).catch(
            (err) => {
                console.log("请求世界排行数据执行失败：", err);
                let promise1 = WXGameService.getWorldRankData(this.appid, this.rankUid, this.rankKey, group, count);
                promise1.then(
                    (res) => {
                        if (callback) {
                            callback(res);
                        }
                    }
                ).catch(
                    (err) => {
                        console.log("请求世界排行数据再次执行失败：", err);
                    }
                );
            }
        );
    }
    //#endregion

    //#region ---好友排行
    // 保存自身好友排行数据 --wx
    setUserCloudStorage(value_v: number) {

        // if (PlayerMgr.getInstance().getUserData().getCurLevel() != 2) {
        //     let curMaxLevelNum = PlayerMgr.getInstance().getUserData().getCurMaxLevel_main();
        //     if (value_v < curMaxLevelNum) {
        //         return;
        //     }
        //     SDKMgr.getInstance().setUserCloudStorage("mainLevel", value_v);
        // SDKMgr.getInstance().setUserCloudStorage(this.rankKey, value_v);
        // }
        EventManager.emit(EventTypes.SDKEvents.SetUserCloudStorage, 'mainLevel', value_v);
    }
    // 获取好友排行数据 --wx
    getFriendCloudStorage(cb?: Function) {
        // SDKMgr.getInstance().getFriendCloudStorage("mainLevel");
        EventManager.emit(EventTypes.SDKEvents.GetFriendCloudStorage, 'mainLevel', (res) => {
            cb && cb(res);
        })

    }
    //#endregion
}
